package com.rtu.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

/**
 * Created by jiafeilu on 2018/3/22.
 */
public class HttpUtil {
    private final static Logger logger = LoggerFactory.getLogger(HttpUtil.class);

    public static String get(String url,String param){
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            logger.error("发送GET请求出现异常", e);
        }
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                logger.error("", e2);
            }
        }
        return result;
    }
    
    public static String post(String url,String param) {
    		PrintWriter out = null;
    		BufferedReader in = null;
    		String result = "";    	
        	
        	try {
    			URL urlObj = new URL(url);
    			URLConnection connection = urlObj.openConnection();
    			connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    	       
    			connection.setDoOutput(true);
    			connection.setDoInput(true);
    			
    			
    			out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(),"UTF-8"));
    			out.print(param);
    			out.flush();
    			
    			in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));   	                   
    	        String line;
    	        while ((line = in.readLine()) != null) {
    	                result += line;}
    	            
    		} catch (Exception e) {
    			e.printStackTrace();
    		}finally {
                try {
                	if (out != null) {
                        out.close();
                    }
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception e2) {
                    logger.error("", e2);
                }
            }
        	
        	return result;
        }
        
    
    
    
    
    
    
    
}
